import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'
import vuex from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
    component: () => import('../views/Index.vue'),
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('../views/Home/index.vue')
      },
      {
        path: '/goods',
        name: 'Goods',
        component: () => import('../views/Goods/index.vue'),
      },
      {
        path: '/goodsDetail/:id',
        name: 'GoodsDetail',
        component: () => import('../views/GoodsDetail'),
        props: true
      },
      {
        path: '/thanks',
        name: 'Thanks',
        component: () => import('../views/Home/thanks.vue'),
      },
    ]
  },
  //#region 
  /* {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home/index.vue')
  },

  {
    path: '/goods',
    name: 'Goods',
    component: () => import('../views/Goods/index.vue'),
  },

  {
    path: '/goodsDetail/:id',
    name: 'GoodsDetail',
    component: () => import('../views/GoodsDetail'),
    props: true
  }, */
  //#endregion
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login')
  },

  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Login/register.vue')
  },

  {
    path: '/user',
    name: 'User',
    component: () => import('../views/User'),
    redirect: '/user/orderList',
    children: [
      { path: 'orderlist', name: 'orderList', component: () => import('../views/User/Children/order.vue'), meta: { auth: true }},
      { path: 'orderDetail/:order_number', name: 'orderDetail', component: () => import('../views/User/Children/orderDetail.vue'), props: true, meta: { auth: true }},
      { path: 'information', name: 'information', component: () => import('../views/User/Children/information.vue'), meta: { auth: true }},
      { path: 'addressList', name: 'addressList', component: () => import('../views/User/Children/addressList.vue'), meta: { auth: true }},
      { path: 'comment', name: 'comment', component: () => import('../views/User/Children/comment.vue'), meta: { auth: true }},
      { path: 'support', name: 'support', component: () => import('../views/User/Children/support.vue'), meta: { auth: true }}
    ],
    meta: { auth: true }
  },

  {
    path: '/cart',
    name: 'Cart',
    component: () => import('../views/Cart'),
    meta: { auth: true }
  },

  {
    path: '/order',
    name: 'order',
    component: () => import('../views/Order/order.vue'),
    children: [
      { path: 'paysuccess', name: 'paysuccess', component: () => import('../views/Order/paysuccess.vue')},
    ],
    meta: { auth: true }
  },

  {
    path: '/checkout',
    name: 'checkout',
    component: () => import('../views/Checkout/checkout.vue'),
    meta: { auth: true }
  },

  { 
    path: '/refreshsearch',
    name: 'refreshsearch',
    component: () => import('../views/Refresh/refreshsearch.vue')
  },
  
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/Search/search.vue')
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const tokenStr = window.sessionStorage.getItem('token')
  const effective = window.sessionStorage.getItem('wuhu')
  const userInfo = window.sessionStorage.getItem('userInfo')

  if(userInfo) {
    vuex.commit('ISLOGIN', JSON.parse(userInfo))
  }
  
  if(to.meta.auth){
    if(!effective) {
      if (!tokenStr) {
        return next('/login')
      }
    } else {
      Message('无效的token')
      window.sessionStorage.clear()
      return next('/login')
    }
  }
  
  next()
})


export default router
